<?php
/*
write by vomtung@yahoo.com.vn
similar class Core_AdminSettingsController
*/
 class Mobileservice_MaintenanceController extends Core_Controller_Action_Standard
{
  public function indexAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
  }
  public function initAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$global_settings_file = APPLICATION_PATH . '/application/settings/general.php';
	$generalConfig = include $global_settings_file;
	echo json_encode(array("result"=>$generalConfig['maintenance']['enabled']));
  }
  public function enablemaintenanceAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$global_settings_file = APPLICATION_PATH . '/application/settings/general.php';
	$generalConfig = include $global_settings_file;
	$maintenance = true;
	$maintenanceCode = $_POST["en4_maint_code"];
	//$maintenanceCode = 'abcde';
	$generalConfig['maintenance']['enabled'] = (bool) $maintenance;
    $generalConfig['maintenance']['code'] = $maintenanceCode;
	setcookie('en4_maint_code', $generalConfig['maintenance']['code'], time() + (60 * 60 * 24 * 365), $this->view->baseUrl());
	if( (is_file($global_settings_file) && is_writable($global_settings_file)) ||
        (is_dir(dirname($global_settings_file)) && is_writable(dirname($global_settings_file))) ) {
      $file_contents = "<?php defined('_ENGINE') or die('Access Denied'); return ";
      $file_contents .= var_export($generalConfig,true);
      $file_contents .= "; ?>";
      file_put_contents($global_settings_file, $file_contents);
      ///$form->addNotice('Your changes have been saved.');
    } else {
      
         echo 'Unable to configure this setting due to the file /application/settings/general.php not having the correct permissions.
                       Please CHMOD (change the permissions of) that file to 666, then try again.';
					   return;
    }
	//echo 'Success , the Page changed to Maintenance Mode';
	echo json_encode(array("result"=>"Success , the Page changed to Maintenance Mode"));
  }
   public function disablemaintenanceAction()
  {
	$this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
	$global_settings_file = APPLICATION_PATH . '/application/settings/general.php';
	$generalConfig = include $global_settings_file;
	$maintenance = false;
    $maintenanceCode = $_POST["en4_maint_code"];
	//$maintenanceCode = 'abcde';
	$generalConfig['maintenance']['enabled'] = (bool) $maintenance;
    $generalConfig['maintenance']['code'] = $maintenanceCode;
	setcookie('en4_maint_code', $generalConfig['maintenance']['code'], time() + (60 * 60 * 24 * 365), $this->view->baseUrl());
	if( (is_file($global_settings_file) && is_writable($global_settings_file)) ||
        (is_dir(dirname($global_settings_file)) && is_writable(dirname($global_settings_file))) ) {
      $file_contents = "<?php defined('_ENGINE') or die('Access Denied'); return ";
      $file_contents .= var_export($generalConfig,true);
      $file_contents .= "; ?>";
      file_put_contents($global_settings_file, $file_contents);
      ///$form->addNotice('Your changes have been saved.');
    } else {
      
         echo 'Unable to configure this setting due to the file /application/settings/general.php not having the correct permissions.
                       Please CHMOD (change the permissions of) that file to 666, then try again.';
					   return;
    }
	echo json_encode(array("result"=>"Disable Maintenance Mode"));
  }
}
 ?>